﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_SellManage_frm_SellContract : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindSellContractByDataView(dst, intPageIndex);
        }
    }

    public void ChildChangPage(object sender, EventArgs e)
    {
        //获取保存的从表中的结果集和DataGrid控件
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;
        DataGrid dgrd = cls_Property.SetOrGetChildDataGridControl;

        if (dst != null && dgrd != null)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = dgrd.CurrentPageIndex;
            int intPageCounts = dgrd.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindSellContractChildByDataView(dgrd, dst, intPageIndex);
        }
    }

    public void AddSellContractInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        ClsMenuDAL cls_Menu = new ClsMenuDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();

            ImageButton ibtnAdd = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnAdd.CommandName);

            if (intCommandName == 0)//打开主表添加面板
            {
                if (!this.panAddResult.Visible)
                {
                    string strCode = cls_Menu.GetSystemCode("0", "00", "", 14);//获取系统编码

                    if (strCode != "Error")
                    {
                        this.panAddResult.Visible = true;
                        this.ctbAdd0.Text = strCode;
                        this.ctsbAdd0.Text =  this.ctbAdd10.Text = DateTime.Now.ToString();
                        this.ctbAdd9.Text = strUserID;
                        this.lblJs.Text = "<script>document.getElementById('" + this.ctsbAdd2.ClientID + "_txtBox').focus();</script>";
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                    }
                }
            }
            else if (intCommandName == 1)//关闭主表添加面板
            {
                this.panAddResult.Visible = false;

                string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAdd0.Text.Trim(), 14);//取消当前系统编码
            }
            else if (intCommandName == 2)//保存主表添加记录
            {
                string Cm_No = this.ctbAdd0.Text.Trim();
                string Cm_Date = this.ctsbAdd0.Text.Trim();
                string Dm_Mdate = this.ctsbAdd1.Text.Trim();
                string Ct_Id = this.ctsbAdd2.Text.Trim();
                string money1 = "0";
                string Dp_Id = this.ctsbAdd4.Text.Trim();
                string Wk_Id = this.ctsbAdd5.Text.Trim();
                string Remark = this.ctbAdd11.Text.Trim();

                Boolean bolIsSucceed = cls_SellManage.AddOrUpdateSellContract(Cm_No, DateTime.Parse(Cm_Date), Dm_Mdate, Decimal.Parse(money1), Ct_Id, Dp_Id, Wk_Id, strUserID, Remark, 0); 

                if (bolIsSucceed)
                {
                    this.GetSellContractInfoByMain();//重新绑定结果

                    //string strCode = cls_Menu.GetSystemCode("0", "00", "", 14);

                    //if (strCode != "Error")
                    //{
                    this.ctsbAdd0.Text = ctsbAdd1.Text = ctsbAdd2.Text = ctsbAdd5.Text = "";
                    this.ctbAdd11.Text = "";
                    //    this.ctbAdd0.Text = strCode;
                    //    this.ctsbAdd0.Text = this.ctsbAdd1.Text = DateTime.Now.ToString();
                    //    this.lblJs.Text = "<script>document.getElementById('" + this.ctsbAdd2.ClientID + "_txtBox').focus();</script>";
                    //}
                    //else
                    //{
                    //    this.panAddResult.Visible = false;
                    //}

                    //主表新增后直接打开从表进行新增从表记录 Begin///////////////////////////
                    this.panAddResult.Visible = false;
                    ImageButton ibtn = new ImageButton();

                    int intItems = this.dgrdResult.Items.Count - 1;

                    for (int i = intItems; i >= 0; i--)
                    {
                        string strRs_DmNO = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                        if (strRs_DmNO == Cm_No)
                        {
                            ibtn = (ImageButton)this.dgrdResult.Items[i].FindControl("ibtnOpenChild");

                            break;
                        }
                    }

                   // this.OpenChildResult(ibtn, null);
                    this.AddSellContractInfo(this.ibtnAdd, null);
                    //主表新增后直接打开从表进行新增从表记录 End///////////////////////////
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                }
            }
            //////////////////////////////////////////从表相关操作/////////////////////////////////////
            else
            {
                int intIndex = Int32.Parse(this.ibtnAdd.CommandArgument);

                Panel panChildAddResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panChildAddResult");

                if (intCommandName == 3)//关闭从表添加面板
                {
                    panChildAddResult.Visible = false;
                }
                else if (intCommandName == 4)//保存从表添加记录
                {
                    CuTextBox Cm_No = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
                    CuTextSelectBox Im_No = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");

                    int intExistCount = cls_SellManage.SellContractChildExistSameMateriel(Cm_No.Text.Trim(), Im_No.Text.Trim());

                    if (true)//该商品不存在则可以添加
                    {
                        CuTextBox Im_name = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd0");
                        CuTextBox Im_size = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd1");
                        CuTextBox Im_unit = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd2");
                        CuTextBox cfee = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd3");
                        CuTextBox Im_price = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd7");
                        //CuTextBox afee = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildRs1");
                        //CuTextBox bfee = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildRs2");
                        
                        CuTextBox Remark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd8");
                        if (Im_size.Text == "") Im_size.Text = "0";
                        if (Im_unit.Text == "") Im_unit.Text = "0";
                        if (cfee.Text == "") cfee.Text = "0";
                        if (Im_price.Text == "") Im_price.Text = "0";
                        Boolean bolIsSucceed = cls_SellManage.AddOrUpdateSellContracChild(0, Cm_No.Text.Trim(), Im_No.Text.Trim(), Decimal.Parse(Im_price.Text.Trim()), int.Parse(Im_size.Text.Trim()), Decimal.Parse(Im_unit.Text.Trim()), cfee.Text.Trim(), Remark.Text.Trim(), 0);

                        if (bolIsSucceed)
                        {
                           // Agio.Text = "1";//清空上一次新增后的值
                            Im_No.Text = Im_name.Text = Im_size.Text = Im_unit.Text = Im_price.Text= Remark.Text = "";

                            DataSet dst = cls_SellManage.GetSellContractDataSetChild(Cm_No.Text.Trim(), 0);//重新绑定从表结果

                            if (dst != null && dst.Tables[0].Rows.Count > 0)
                            {
                                DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");

                                this.BindSellContractChildByDataView(dgrd, dst, 0);
                            }

                            ((ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild")).Attributes.Remove("OnClick");

                            this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                        }
                    }
                    else
                    {
                        if (intExistCount > 0)
                        {
                            this.lblJs.Text = "<script>alert('该商品已存在！')</script>";
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                        }

                        this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').select();document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                    }
                }
                else if (intCommandName == 5)//打开从表添加面板
                {
                    panChildAddResult.Visible = true;

                    CuTextSelectBox ctsbChildAdd0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");
                    this.lblJs.Text = "<script>document.getElementById('" + ctsbChildAdd0.ClientID + "_txtBox').focus();</script>";
                }
            }
        }
        else
        {
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAdd0.Text.Trim(), 14);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void UpDateSellContractInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ImageButton ibtnUpdate = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnUpdate.CommandName);
            int intIndex = Int32.Parse(ibtnUpdate.CommandArgument);

            if (intCommandName == 0)//主表确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        string strCm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                        ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();

                        //判断此合同订单是否存在子单，不存在则不允许确认
                        //int intExistCount = cls_SellManage.SellContractExistState(strCm_No, 2);
                        int intExistCount = 1;
                        if (intExistCount != 0)
                        {
                           // this.lblJs.Text = "<script>ModalDialog('../Mod_QueryFrom/frm_Audit.aspx?PageID=" + this.hidCurrPageID.Value + "&C_No=" + strCm_No + "', 463, 390);</script>";
                            Boolean bolIsSucceed = cls_SellManage.UpdateSellContractState(strCm_No, 0, strUserID);

                            if (!bolIsSucceed)
                            {
                                this.lblJs.Text = "<script>alert('确认失败，请重试！')</script>";
                            }
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('无效合同订单不能确认！')</script>";
                        }
                    }
                }

                this.GetSellContractInfoByMain();//重新绑定结果
            }
            else if (intCommandName == 3)//主表取消确认
            {
                string strCm_No = ((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0")).Text.Trim();

                ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();

                //判断此合同订单是否存在于销售发货，是则不允许取消确认
                //int intExistCount = cls_SellManage.SellContractExistState(strCm_No, 1);

                if (true)
                {
                    Boolean bolIsSucceed = cls_SellManage.UpdateSellContractState(strCm_No, 1, strUserID);

                    if (bolIsSucceed)
                    {
                        this.GetSellContractInfoByMain();//重新绑定结果
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('取消确认失败，请重试！')</script>";
                    }
                }
                else
                {
                    this.lblJs.Text = "<script>alert('此单不允许取消确认，请先作废此单销售发货记录！')</script>";
                }
            }
            else if (intCommandName == 1 || intCommandName == 2 || intCommandName == 4)//主表编辑
            {
                CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
                ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
                ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
                ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
                ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
                CuTextBox ctbRs0 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
                CuTextBox ctbRs1 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs1");
                CuTextBox ctbRs2 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs2");
                CuTextBox ctbRs3 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs3");
                CuTextBox ctbRs4 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs4");
                CuTextBox ctbRs5 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs5");
                CuTextBox ctbRs6 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs6");
                TextBox money1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("money1");
                CuTextBox ctbRs7 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs7");
                CuTextBox ctbRs8 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs8");
                CuTextBox ctbRs9 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs9");
                CuTextBox ctbRs10 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs10");
                CuTextBox ctbRs11 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs11");
                CuTextSelectBox ctsbRs0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs0");
                TextBox ctsbRs1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs1");
                CuTextSelectBox ctsbRs2 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs2");
                CuTextSelectBox ctsbRs3 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs3");
                CuTextSelectBox ctsbRs4 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs4");
                CuTextSelectBox ctsbRs5 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs5");

                if (intCommandName == 1)//取消编辑
                {
                    this.GetSellContractInfoByMain();//重新绑定结果
                }
                else if (intCommandName == 2)//编辑
                {
                    ctsbRs0.Text = ctsbRs0.ToolTip;
                    ctsbRs1.Text = ctsbRs1.ToolTip;
                    ctsbRs2.Text = ctsbRs2.ToolTip;
                    //ctsbRs3.Text = ctsbRs3.ToolTip;
                    ctsbRs4.Text = ctsbRs4.ToolTip;
                    ctsbRs5.Text = ctsbRs5.ToolTip;
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                    chkRsSelect.Visible = ibtnRsEdit.Visible = ibtnRsOAudit.Visible = false;
                    ctbRs11.ReadOnly = false; ctsbRs1.BorderWidth = money1.BorderWidth = 1;
                     money1.ReadOnly =ctsbRs0.ReadOnly = ctsbRs1.ReadOnly = ctsbRs2.ReadOnly = ctsbRs4.ReadOnly = ctsbRs5.ReadOnly = false;
                    this.lblJs.Text = "<script>document.getElementById('" + ctsbRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbRs0.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();
                    Boolean bolIsSucceed = cls_SellManage.AddOrUpdateSellContract(ctbRs0.Text.Trim(), DateTime.Parse(ctsbRs0.Text.Trim()),ctsbRs1.Text.Trim(), Decimal.Parse(money1.Text .Trim()), ctsbRs2.Text.Trim(), ctsbRs4.Text.Trim(), ctsbRs5.Text.Trim(), strUserID, ctbRs11.Text.Trim(), 1);
                   
                    if (bolIsSucceed)
                    {
                        this.GetSellContractInfoByMain();//重新绑定结果
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('修改记录失败，请重试！')</script>";
                    }
                }
            }
            //////////////////////////////////////////从表相关操作/////////////////////////////////////
            else
            {
                ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();
                DataGrid dgrd = new DataGrid();
                string strCm_No = "";
                int intItems = this.dgrdResult.Items.Count;

                for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        strCm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                        break;
                    }
                }

                CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[intIndex].FindControl("chkChildRsSelect");//在子DataGrid中找要操作的控件
                ImageButton ibtnChildRsCancel = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsCancel");
                ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsEdit");
                ImageButton ibtnChildRsServer = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsServer");
                CuTextSelectBox ctsbChildRs0 = (CuTextSelectBox)dgrd.Items[intIndex].FindControl("ctsbChildRs0");
                CuTextBox ctbChildRs4 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs4");
                CuTextBox ctbChildRs1 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs1");
                CuTextBox ctbChildRs2 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs2");
                CuTextBox ctbChildRs3 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs3");
                CuTextBox ctbChildRs7 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs7");
                CuTextBox ctbChildRs8 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs8");

                if (intCommandName == 5)//取消编辑
                {
                    DataSet dst = cls_SellManage.GetSellContractDataSetChild(strCm_No, 0);

                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                    {
                        this.BindSellContractChildByDataView(dgrd, dst, 0);
                    }
                }
                else if (intCommandName == 6)//编辑
                {
                    ctbChildRs1.ReadOnly = ctbChildRs2.ReadOnly = ctbChildRs3.ReadOnly = false;
                    chkChildRsSelect.Visible = ibtnChildRsEdit.Visible = false;
                    ibtnChildRsCancel.Visible = ibtnChildRsServer.Visible = true;
                    ctsbChildRs0.ReadOnly = ctbChildRs8.ReadOnly = ctbChildRs7.ReadOnly = false;
                    this.lblJs.Text = "<script>document.getElementById('" + ctsbChildRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbChildRs0.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    int intExistCount = (ctsbChildRs0.Text.Trim() == ctsbChildRs0.ToolTip.Trim()) ? 0 : cls_SellManage.SellContractChildExistSameMateriel(strCm_No, ctsbChildRs0.Text.Trim());

                    if (intExistCount == 0)//该商品不存在则可以修改
                    {
                        int intID = Int32.Parse(dgrd.Items[intIndex].Cells[1].Text.Trim());

                        Boolean bolIsSucceed = cls_SellManage.AddOrUpdateSellContracChild(intID, strCm_No, ctsbChildRs0.Text.Trim(), decimal.Parse(ctbChildRs7.Text.Trim()), int.Parse(ctbChildRs1.Text.Trim()), decimal.Parse(ctbChildRs2.Text.Trim()), ctbChildRs3.Text.Trim(), ctbChildRs8.Text.Trim(), 1);

                        if (bolIsSucceed)//重新绑定从表结果
                        {
                            DataSet dst = cls_SellManage.GetSellContractDataSetChild(strCm_No, 0);

                            if (dst != null && dst.Tables[0].Rows.Count > 0)
                            {
                                this.BindSellContractChildByDataView(dgrd, dst, 0);
                            }
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('修改记录失败，请重试！')</script>";
                        }
                    }
                    else
                    {
                        if (intExistCount > 0)
                        {
                            this.lblJs.Text = "<script>alert('该商品已存在！')</script>";

                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('修改记录失败，请重试！')</script>";
                        }

                        this.lblJs.Text = "<script>document.getElementById('" + ctsbChildRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbChildRs0.ClientID + "_txtBox').focus();</script>";
                    }
                }
            }
        }
        else
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAdd0.Text.Trim(), 14);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    public void OpenChildResult(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnOpen = (ImageButton)sender;
        int intCommandName = Int32.Parse(ibtnOpen.CommandName);
        int intIndex = Int32.Parse(ibtnOpen.CommandArgument);
        int intCount = this.dgrdResult.Items.Count;
        bool is_audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text);

        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnOpenChild = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
        Panel panShowChildResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panShowChildResult");

        //获取从表信息
        CuTextBox ctbRs0 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
        ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();
        DataSet dst = cls_SellManage.GetSellContractDataSetChild(ctbRs0.Text.Trim(), 0);

        if(dst == null || dst.Tables[0].Rows.Count <= 0)
        {
            ibtnOpen.Attributes.Add("OnClick", "return confirm('单据录入未完成，您要作废此单据吗？');");
        }

        if (intCommandName == 0)//展开
        {
            for (int i = 0; i < intCount; i++)//找到展开的项
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (i == intIndex)
                {
                    panItem.Visible = true;
                }
                else
                {
                    panItem.Visible = false;
                }
            }

            this.ibtnAdd.CommandName = "5";//主表展开后则不可操作
            this.ibtnRef.CommandName = "1";
            this.ibtnAdd.CommandArgument = intIndex.ToString();
            panShowChildResult.Visible = true;
            ibtnOpenChild.CommandName = "1";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/10.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/16.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/18.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_3.gif";
            this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;

            if (is_audit)//如果主表确认后则从表不可操作
            {
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = false;
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_3.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_5.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_6.gif";
                this.imgDelete.Attributes.Add("OnMouseOver", "");
                this.imgDelete.Attributes.Add("OnMouseOut", "");
                this.imgDelete.Attributes.Add("OnClick", "");
            }

            //显示从表信息
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");

                this.BindSellContractChildByDataView(dgrd, dst, 0);//绑定从表信息
            }
        }
        else//收缩
        {
            this.ibtnAdd.CommandName = "0";
            this.ibtnRef.CommandName = "0";

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                panShowChildResult.Visible = false;
                ibtnOpenChild.CommandName = "0";
                ibtnOpenChild.ImageUrl = "../App_Images/ico/9.gif";
                ibtnRsEdit.ImageUrl = "../App_Images/ico/12.gif";
                ibtnRsServer.ImageUrl = "../App_Images/ico/11.gif";
                ibtnRsOAudit.ImageUrl = "../App_Images/ico/8.gif";
                ibtnRsCancel.ImageUrl = "../App_Images/ico/4.gif";
                this.ibtnAudit.ImageUrl = "../App_Images/b_21_1.gif";
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_1.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_1.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_4.gif";
                this.imgDelete.Attributes.Add("OnMouseOver", "ChangImage(this, '../App_Images/b_19_3.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_2.gif')");
                this.imgDelete.Attributes.Add("OnMouseOut", "ChangImage(this, '../App_Images/b_19_4.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_1.gif')");
                this.imgDelete.Attributes.Add("OnClick", "ChildMenuVisible()");
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = true;
            }
            else
            {
                Boolean is_Succeed = cls_SellManage.UpdateSellContractState(ctbRs0.Text.Trim(), 3, "");
            }

            this.GetSellContractInfoByMain();//重新绑定数据
        }
    }

    private void GetSellContractInfoByMain()
    {
        ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();

        DataSet dst = cls_SellManage.GetSellContractDataSet(0);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindSellContractByDataView(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindSellContractByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    private void BindSellContractChildByDataView(DataGrid dgrd, DataSet dst, int CurrPageIndex)
    {
        if (dgrd != null && dst != null)
        {
            dgrd.CurrentPageIndex = CurrPageIndex;
            dgrd.DataSource = dst;
            dgrd.DataBind();

            int intIndex = Int32.Parse(dgrd.ToolTip);
            int intItems = dgrd.Items.Count;

            Label lblChildRsCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildRsCounts");
            Label lblChildPageCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildPageCounts");
            Label lblChildCurrPage = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildCurrPage");
            LinkButton lbtnChildFirst = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildFirst");
            LinkButton lbtnChildPrev = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildPrev");
            LinkButton lbtnChildNext = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildNext");
            LinkButton lbtnChildEnd = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildEnd");

            lblChildRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条明细记录";
            lblChildPageCounts.Text = "共" + dgrd.PageCount + "页";
            lblChildCurrPage.Text = "当前第" + (dgrd.CurrentPageIndex + 1) + "页";
            lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = true;
            lblChildRsCounts.Enabled = lblChildPageCounts.Enabled = lblChildCurrPage.Enabled = true;

            if (dgrd.PageCount == 1)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == dgrd.PageCount - 1)
            {
                lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == 0)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = false;
            }

            //判断从表中的记录是否可操作
            for (int i = 0; i < intItems; i++)
            {
                CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[i].FindControl("chkChildRsSelect");
                ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[i].FindControl("ibtnChildRsEdit");

                if (!this.ibtnAdd.Enabled)
                {
                    chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
                    ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
                }
                else
                {
                    chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = true;
                    ibtnChildRsEdit.ImageUrl = "../App_Images/ico/12.gif";
                }
            }

            //保存从表中的结果集和DataGrid控件用于从表中的翻页
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetChildResultDataSet = dst;
            cls_Property.SetOrGetChildDataGridControl = dgrd;
        }
    }

    private void DeleteStockCheckInfo(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();
            int intCount = this.dgrdResult.Items.Count;
            int intcommand = Int32.Parse(this.ibtnRef.CommandName);

            if (intcommand == 0)//作废主表
            {
                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        Boolean bolIsSucceed = false;
                        string strCm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                        if (CommandName == 0)//不彻底作废
                        {
                            bolIsSucceed = cls_SellManage.UpdateSellContractState(strCm_No, 2, strUserID);
                        }
                        else//彻底作废
                        {
                            bolIsSucceed = cls_SellManage.UpdateSellContractState(strCm_No, 3, strUserID);
                        }

                        if (!bolIsSucceed)
                        {
                            this.lblJs.Text = "<script>alert('作废记录失败，请重试！')</script>";
                        }
                    }
                }

                //重新绑定主表结果
                this.GetSellContractInfoByMain();
            }
            else//作废从表
            {
                DataGrid dgrd = new DataGrid();
                string strCm_No = "";
                int intItems = this.dgrdResult.Items.Count;

                for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        strCm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                        break;
                    }
                }

                int intChildItems = dgrd.Items.Count;//循环作废所选的项

                for (int j = intChildItems - 1; j >= 0; j--)
                {
                    bool IsChecked = ((CheckBox)dgrd.Items[j].FindControl("chkChildRsSelect")).Checked;

                    if (IsChecked)
                    {
                        int intIndex = Int32.Parse(dgrd.Items[j].Cells[1].Text.Trim());
                        Boolean bolIsSucceed = false;

                        if (CommandName == 0)//不彻底作废
                        {
                            bolIsSucceed = cls_SellManage.UpdateSellContractStateChild(intIndex, 2);
                        }
                        else//彻底作废
                        {
                            bolIsSucceed = cls_SellManage.UpdateSellContractStateChild(intIndex, 3);
                        }

                        if (!bolIsSucceed)
                        {
                            this.lblJs.Text = "<script>alert('作废记录失败，请重试！')</script>";
                        }
                    }
                }

                DataSet dst = cls_SellManage.GetSellContractDataSetChild(strCm_No, 0);//重新绑定从表结果

                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    this.BindSellContractChildByDataView(dgrd, dst, 0);
                }
                else
                {
                    dgrd.CurrentPageIndex = 0;
                    dgrd.DataSource = null;
                    dgrd.DataBind();
                }
            }
        }
        else
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAdd0.Text.Trim(), 14);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                { 
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.hidCurrPageID.Value = strCurrPageID;
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetSellContractInfoByMain();
            }
            else
            {
                this.lblJs.Text = "";//清空Js脚本
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
        }
        else
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAdd0.Text.Trim(), 14);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindSellContractByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

        if (intCommand == 0)//刷新主表
        {
            this.GetSellContractInfoByMain();
        }
        else//刷新从表
        {
            DataGrid dgrd = new DataGrid();
            string strCm_No = "";
            int intItems = this.dgrdResult.Items.Count;

            for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (panItem.Visible)
                {
                    dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                    strCm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                    break;
                }
            }

            ClsPlanBackDAL cls_SellManage = new ClsPlanBackDAL();
            DataSet dst = cls_SellManage.GetSellContractDataSetChild(strCm_No, 0);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.BindSellContractChildByDataView(dgrd, dst, 0);
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool Is_audit = bool.Parse(e.Item.Cells[1].Text.Trim());
            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");
            ImageButton ibtnRsOAudit = (ImageButton)e.Item.FindControl("ibtnRsOAudit");

            if (!Is_audit)
            {
                if (strButtonEdit.Trim() == "1")
                {
                    if (this.panAudit.Visible || this.panDelete.Visible)
                    {
                        chkRsSelect.Visible = true;
                    }

                    ibtnRsEdit.Visible = true;
                }
                else if (this.panAudit.Visible || this.panDelete.Visible)
                {
                    chkRsSelect.Visible = true;
                }
            }
            else
            {
                ibtnRsOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnRsOAudit.Enabled = false;
                    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }
            }
        }
    }

    protected void dgrdResult_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType.ToString() == "Item" || e.Item.ItemType.ToString() == "AlternatingItem")
        {
            DataGrid dgrdChildResult = (DataGrid)e.Item.FindControl("dgrdChildResult");

            //子DataGrid注册事件
            dgrdChildResult.ItemDataBound += new DataGridItemEventHandler(dgrdChildResult_ItemDataBound);
        }
    }

    protected void dgrdChildResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            //decimal ctbChildRs3 = decimal.Parse(((CuTextBox)e.Item.FindControl("ctbChildRs3")).Text.Trim());
      
            
           
          
        }
    }
}
